package com.intranet.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.sys.domain.Menu;
import com.intranet.sys.service.MenuService;
import com.water.web.Tool;
/**
 * 菜单
 * @author Administrator
 *
 */
@Controller
public class MenuController {

	@Autowired
	private MenuService menuService;

	/**
	 * 查询所有菜单
	 * 
	 * @param response
	 * @param page
	 * @param rows
	 */
	@RequestMapping("/getAllMenus")
	public ModelAndView getAllMenus(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/sys/menu/menuList");
		return mav;
	}

	@RequestMapping("/saveMenu")
	public void saveMenu(HttpServletResponse response, Menu menu) {
		ModelAndView mav = new ModelAndView();
		String msg = "";
		if (menu.getId() == null) {
			Menu saveMenu = new Menu();
			saveMenu.setParentId(menu.getParentId());
			saveMenu.setName(menu.getName());
			saveMenu.setUrl(menu.getUrl());
			msg = menuService.saveMenu(saveMenu);
			msg = "Y";
		} else {
			Menu m = menuService.findMenu(menu.getId());
			m.setParentId(menu.getParentId());
			m.setName(menu.getName());
			m.setUrl(menu.getUrl());
			msg = menuService.updateMenu(m);
			msg = "Y";
		}
		JSONObject o = new JSONObject();
		o.put("Message", msg);
		o.put("ReturnUrl", msg);
		Tool.writeByAction(o.toString(), response);
	}

	@RequestMapping("/deleteMenu")
	public void deleteMenu(HttpServletResponse response, @RequestParam("id")
	int id) {
		String msg = "";
		List<Menu> menus = menuService.findMenuByPId(id);
		for (Menu menu : menus) {
			menuService.deleteMenu(menu);
		}
		Menu m = menuService.findMenu(id);
		msg = menuService.deleteMenu(m);
		Tool.writeByAction(msg.toString(), response);
	}

	/**
	 * 加载菜单
	 * 
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping("/loadManu")
	public ModelAndView loadManu(HttpServletResponse response,
			@RequestParam("id")
			int id) {
		ModelAndView mav = new ModelAndView();
		Menu menu = menuService.findMenu(id);
		mav.addObject("menu", menu);
		mav.setViewName("/sys/menu/loadMenu");
		return mav;
	}

	/**
	 * 菜单内容根Tree
	 * 
	 * @param response
	 */
	@RequestMapping("/getTreeMenus")
	public void getTreeMenus(HttpServletResponse response, @RequestParam("pid")
	int pid) {
		StringBuffer sb = new StringBuffer();
		List<Menu> menus = menuService.findMenuByPId(pid);
		for (Menu m : menus) {
			sb.append("<ul class='branch in'>");
			sb.append("<li>");
			if (m.getMenus().size() > 0) {
				sb
						.append("<a  href='getTreeMenus.do?pid="
								+ m.getId()
								+ "' data-role='branch' class='tree-toggle closed' data-toggle='branch' data-value='JSON_Example'>");
				sb.append(m.getName());
				sb.append("</a>");
			} else {
				sb.append("<a href='#'>");
				sb.append("<i class='icon-suitcase'></i>");
				sb.append(m.getName());
				sb.append("</a>");
			}
			sb.append("</li>");
			sb.append("</ul>");
		}

		Tool.writeByAction(sb.toString(), response);
	}

	/**
	 * 菜单内容根
	 * 
	 * @param response
	 */
	@RequestMapping("/getJSONMenus")
	public void getJSONMenus(HttpServletResponse response, @RequestParam("pid")
	int pid) {
		StringBuffer sb = new StringBuffer();
		List<Menu> menus = menuService.findMenuByPId(pid);
		for (Menu m : menus) {
			sb.append("<ul class='branch in'>");
			sb.append("<li>");
			if (m.getMenus().size() > 0) {
				sb
						.append("<a onclick='showMenu("
								+ m.getId()
								+ ")' href='getJSONMenus.do?pid="
								+ m.getId()
								+ "' data-role='branch' class='tree-toggle closed' data-toggle='branch' data-value='JSON_Example'>");
				sb.append(m.getName());
				sb.append("</a>");
			} else {
				sb.append("<a onclick='showMenu(" + m.getId() + ")' href='#'>");
				sb.append("<i class='icon-suitcase'></i>");
				sb.append(m.getName());
				sb.append("</a>");
			}
			sb.append("</li>");
			sb.append("</ul>");
		}

		Tool.writeByAction(sb.toString(), response);
	}

	/**
	 * 菜单内容
	 * 
	 * @param response
	 */
	@RequestMapping("/getJSONMenuTree")
	public void getJSONMenuTree(HttpServletResponse response) {
		JSONArray item = new JSONArray();
		List<Menu> menus = menuService.findMenuByPId(0);
		for (Menu m : menus) {
			JSONObject o = new JSONObject();
			o.put("id", m.getId());
			o.put("name", m.getName());
			o.put("ename", m.getName());
			o.put("open", false);
			if (m.getMenus().size() > 0) {
				o.put("nodes", getJSONMenuTree2(m.getId()));
			}
			item.add(o);
		}
		Tool.writeByAction(item.toString(), response);
	}

	public JSONArray getJSONMenuTree2(Integer id) {
		List<Menu> menus = menuService.findMenuByPId(id);
		JSONArray item = new JSONArray();
		for (Menu m : menus) {
			JSONObject o = new JSONObject();
			o.put("id", m.getId());
			o.put("name", m.getName());
			o.put("ename", m.getName());
			if (m.getMenus().size() > 0) {
				o.put("isParent", true);
				o.put("nodes", getJSONMenuTree2(m.getId()));
			}
			item.add(o);
		}
		return item;
	}
}
